package com.cgly.medical.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cgly.medical.domain.Account;
import com.cgly.medical.domain.Doctor;
import com.cgly.medical.excel.DoctorData;
import com.cgly.medical.service.DoctorService;

/**
 * @author wangxiangli
 * @create 2022-07-22-14:03
 */
public class DoctorExcelListener extends AnalysisEventListener<DoctorData> {

    //因为SubjectExcelListener不能交给spring进行管理，需要自己new，不能注入其他对象
    //不能实现数据库操作
    public DoctorService doctorService;
    public DoctorExcelListener() {
    }

    public DoctorExcelListener(DoctorService doctorService) {
            this.doctorService=doctorService;
    }


    @Override
    public void invoke(DoctorData doctorData, AnalysisContext analysisContext) {
        String phone = doctorData.getPhone();
        Account account = doctorService.selectAccountByPhone(phone);
        if(account!=null){
            Doctor doctor = new Doctor();
            doctor.setName(account.getRealname());
            doctor.setPhone(phone);
            doctor.setAge(doctorData.getAge());
            doctor.setSex(doctorData.getSex());
            doctor.setLevelId(doctorData.getLevelId());
            doctor.setTypeId(doctorData.getTypeId());
            doctor.setAccountId(account.getId().intValue());
            doctor.setHospital(doctorData.getHospital());
            Doctor doctor1 = doctorService.selectDoctorByPhone(phone);
            if(doctor1==null){
                doctorService.addDoctorinfo(doctor);
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
